<?php
class ModelFaPurchaseOrder extends Model {

    public function addOrder($data){
        $this->db->query("INSERT INTO " . DB_PREFIX . "fa_purchase_order SET supplier_id = '" . (int)$data['supplier_id'] . "' , store_id = '" . (int)$data['store'] . "', order_code = '" . $this->db->escape($data['order_code']) . "' ,order_date = '" . $data['order_date'] . "' , note= '" . $this->db->escape($data['note']) . "'");

        $order_id = $this->db->getLastId();
        if (isset($data['items'])) {
            foreach ($data['items'] as $item) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "fa_purchase_order_detail SET order_id = '" . (int)$order_id . "' , items = '" . $item['item_id'] . "'");
            }    
        }
        $this->cache->delete('fa_purchase_order');
    }

    public function editOrder($order_id, $data){
        $this->db->query("UPDATE " . DB_PREFIX . "fa_purchase_order SET supplier_id = '" . (int)$data['supplier_id'] . "' , store_id = '" . (int)$data['store'] . "', order_code = '" . $this->db->escape($data['order_code']) . "' ,order_date = '" . $data['order_date'] . "' , note= '" . $this->db->escape($data['note']) . "'  WHERE order_id = '" . (int)$order_id . "'");

        $this->cache->delete('fa_purchase_order');
    }

    public function deleteOrder($order_id) {
        $this->db->query("DELETE FROM " . DB_PREFIX . "fa_purchase_order WHERE order_id = '" . (int)$order_id . "'");

        $this->cache->delete('fa_purchase_order');
    }

    public function getOrder($order_id){
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "fa_purchase_order WHERE order_id = '" . (int)$order_id . "'");
        return $query->row;
    }

    public function getOrders(){
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "fa_purchase_order");
        return $query->rows;
    }

    public function getPurchase($supplier_id){
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "fa_purchase_price WHERE supplier_id = '" . (int)$supplier_id . "'");
        
        return $query->rows;        
    }
    
    public function getItem($item_id) {
        $query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'item_id=" . (int) $item_id . "') AS keyword FROM " . DB_PREFIX . "fa_item p LEFT JOIN " . DB_PREFIX . "fa_item_description pd ON (p.item_id = pd.item_id) WHERE p.item_id = '" . (int) $item_id . "' AND pd.language_id = '" . (int) $this->config->get('config_language_id') . "'");

        return $query->row;
    }

    public function getAddressSupplier($supplier_id){
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "fa_supplier WHERE supplier_id = '" . (int)$supplier_id . "'");
        
        return $query->row; 
    }

    public function getTotalOrders() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "fa_purchase_order");
        
        return $query->row['total'];
    }   

    
}
?>
